/*
 * This file is part of the Boring Engine's Kirby Game.
 *
 * Copyright (c) 2011 by pf5234 <pf5234@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package kirby;

import com.googlecode.boringengine.Font;
import com.googlecode.boringengine.Graphics;
import com.googlecode.boringengine.Main;
import com.googlecode.boringengine.Render;
import com.googlecode.boringengine.lua.LuaInterpreter;

public class HUD {

    private static Font font;
    private static Kirby kirby;
    private static Enemy lastEnemy;
    private static String lastEnemyType = "No Enemy";
    private static int lastEnemyTypeWidth;
    private static String debugString;
    private static String levelName = "";
    
    public static void init(Kirby k, Font f) {
        kirby = k;
        font = f;
        lastEnemyTypeWidth = font.getWidth(lastEnemyType);
    }

    public static void draw() {
        int oldalpha = Render.getAlpha(); Render.setAlpha(255);
        if (lastEnemy != null && lastEnemy.isDead()) setLastEnemy(null);
        Render.moveOverWithoutScale(0, 557);
        Render.drawRect(800, 43, 9);
        Render.drawRect(800, 2, 0);
        Render.moveOverWithoutScale(0, -557);
        double oldScale = Render.getScale();
        font.draw(0, 560, "KIRBY");
        Render.addScale(2);
        int health = kirby.getHealth();
        if (health < 1) health = 0;
        font.draw(0, 575, "" + health);
        Render.setScale(oldScale);
        font.draw(font.getWidth("" + health) * 2, 587, "/" + kirby.getMaxHealth());
        font.draw(600 - (int)(lastEnemyTypeWidth*oldScale), 560, lastEnemyType);
        if (lastEnemy != null) {
            Render.addScale(2);
            health = lastEnemy.getHealth();
            if (health < 1) health = 0;
            int w1 = font.getWidth("" + health) * 2;
            int w2 = font.getWidth("/" + lastEnemy.getMaxHealth());
            font.draw(600 - w1 - w2, 575, "" + health);
            Render.setScale(oldScale);
            font.draw(600 - w2, 587, "/" + lastEnemy.getMaxHealth());
        }
        String str = Graphics.getFPSString();
        font.draw(300 - font.getWidth(str) / 2, 587, str);
        font.draw(300 - font.getWidth(levelName) / 2, 560, levelName);
        if (Main.DEBUG && debugString != null) {
            String[] strs = debugString.split("\n");
            for (int i = 0; i < strs.length; i++)
                font.draw(0, i*15, strs[i]);
            alreadySetThisFrame = false;
        }
        Render.setAlpha(oldalpha);
    }

    public static void setLastEnemy(Enemy e) {
        if (e == lastEnemy) return;
        if (e == null) {
            lastEnemyType = "No Enemy";
            LuaInterpreter.deleteObject(lastEnemy);
        } else {
            lastEnemyType = e.getType();
            LuaInterpreter.addObject(e, "lastEnemy");
        }
        lastEnemy = e;
        if (font != null)
            lastEnemyTypeWidth = font.getWidth(lastEnemyType);
    }

    private static boolean alreadySetThisFrame;
    public static void setDebugString(String str) {
        if (alreadySetThisFrame)
            debugString += "\n" + str;
        else
            debugString = str;
        alreadySetThisFrame = true;
    }

    public static void setLevelName(String name) {
        levelName = name.toUpperCase();
    }
}
